#include "chooselevelscene.h"
#include "mybutton.h"
#include <QLabel>
#include <QMenuBar>
#include <QPainter>
#include <QSound>
#include <QTimer>

ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    //设置固定大小
    this->setFixedSize(320,588);
    //设置图标
    this->setWindowIcon(QIcon(":/Coin0001.png"));
    //设置标题栏
    this->setWindowTitle("选择关卡");

    //添加菜单栏
    QMenuBar * bar = menuBar();
    setMenuBar(bar);
    QMenu * startMenu = bar->addMenu("开始");
    QAction * quit = startMenu->addAction("退出");
    connect(quit,&QAction::triggered,this,&ChooseLevelScene::close);

    //返回按钮
    MyButton * backBtn = new MyButton(":/BackButton.png",":/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width() - backBtn->width(),this->height() - backBtn->height());

    QSound * backSound = new QSound(":/BackButtonSound.wav");
    QSound * startSound = new QSound(":/TapButtonSound.wav");

    connect(backBtn,&MyButton::clicked,[=](){
        //qDebug()<<"返回";

        backSound->play();

        QTimer::singleShot(500,[=](){
            emit this->chooseSceneBack();
        });
    });

    //创建选择关卡的按钮
    for(int i=0; i<20; i++){
        MyButton * menuBtn = new MyButton(":/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(25+i%4*70,130+i/4*70);

        connect(menuBtn,&MyButton::clicked,[=](){
//           QString str = QString("您选择的是 %1 关").arg(i+1);
//           qDebug()<<str;
           //进入游戏场景

            startSound->play();

           play = new PlayScene(i+1);
           this->hide();
           play->show();

           connect(play,&PlayScene::chooseSceneBack,[=](){
               play->close();
               this->show();
               delete play;
               play = NULL;
           });
        });

        QLabel * label = new QLabel;
        label->setParent(this);
        label->setFixedSize(menuBtn->width(),menuBtn->height());
        label->setText(QString::number(i+1));
        label->move(25+i%4*70,130+i/4*70);
        label->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }

}

void ChooseLevelScene::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;

    //加载背景
    pix.load(":/OtherSceneBg.png");
    //绘制背景
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //加载图标
    pix.load(":/Title.png");
    //图标缩放
    pix = pix.scaled(pix.width() * 0.5,pix.height() * 0.5);
    //绘制图标
    painter.drawPixmap(10,30,pix);
}
